/**
 * @program: clms-second
 * @description:
 * @author: mitu_echo
 * @create: 2021-07-31 16:58
 **/

package com.second.alipay.service.impl;


import com.alipay.api.AlipayApiException;
import com.second.alipay.service.AlipayService;
import com.second.alipay.pojo.utils.AlipayUtils;
import com.second.common.pojo.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class AlipayServiceImpl implements AlipayService {

    @Override
    public R payMoney(Integer orderMoney) throws Exception {

        String orderId = UUID.randomUUID().toString();
        String code = AlipayUtils.getCode(orderId,orderMoney.toString());
        List list = new ArrayList();
        list.add(code);
        list.add(orderId);
        return R.success().putListData(list);
    }

    @Override
    public R getAliPayStatus(String orderId) throws AlipayApiException {

        if (StringUtils.isNotEmpty(orderId)) {
            // 根据订单号去查询支付宝订单状态
            Map<String, String> map = AlipayUtils.getAliPayStatus(orderId);
            if (map.get("payType").equals("SUCCESS")) {
                return R.success().putObject(map);
            } else {
                return R.error();
            }
        }
        return R.error();
    }
}
